package com.leyou.item.service;

import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: taft
 * @Date: 2018-12-17 11:00
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> queryByParentId(Long pid) {

        Category category = new Category();
        category.setParentId(pid);
        return this.categoryMapper.select(category);
    }

    public List<Category> queryByBrandId(Long bid) {
        return this.categoryMapper.queryByBrandId(bid);
    }

    public void deleteByBrandId(Long did) {
        this.categoryMapper.deleteById(did);
        this.categoryMapper.deleteByBrandId(did);
    }

    public List<String> queryNamesByIds(List<Long> cids) {

        //根据一个集合列表去查询到一个结合结果
        List<Category> categories = this.categoryMapper.selectByIdList(cids);

        List<String> names = new ArrayList<>();
        categories.forEach(category -> {
            names.add(category.getName());
        });

        return names;
    }
}
